const app = getApp()

import {
  getOrderList
} from '../../api/user'

Page({

  /** 页面的初始数据 */
  data: {
    state: 1,
    pageNo: 1,
    dataArr: [],
    userHeader: "",     // 用户头像
    userName: "",       // 用户昵称
    hidden1: "inherit",
    hidden2: "hidden",
    hidden3: "hidden",
    contentHeight:0,
    bottomIsLoadingHidden: false,
    isLoadingText: '加载中...',
    earningsHidden: true,         // userType=2才有分享
    isEmptyData: true
  },

  onLoad: function () {
    this.setData({
     contentHeight:(tt.getSystemInfoSync().windowHeight+150) +"rpx"
    })
    tt.setNavigationBarTitle({
      title: '贝优阁',
    })
    this.readUserInfo()     // 读取用户头像和昵称信息
    this.loadMore()
  },

  onShow: function () {
    console.log("-------page-----onShow------------");
  },

  /** 读取用户信息 */
  readUserInfo: function () {
    let that = this;
    tt.getStorage({
      key: "userInfo",
      success(res) {
        console.log(`getStorage`);
        console.log(res);
        that.setData({
          userHeader: res.data.avatarUrl,
          userName: res.data.nickName,
        });
      },
      fail(res) {
        console.log("getStorage调用失败");
      },
    });
    
    this.setData({
      earningsHidden: app.globalData.userType != 2
    })

  },

  /** 列表item点击事件 */
  itemOnClick: function () {
    tt.navigateTo({

    });
  },

  /** 退款 */
  refundClick: function (e) {
    let goodsInfo = e.currentTarget.dataset.item
    let orderNum = goodsInfo.orderId
    let goodsNum = goodsInfo.num
    let goodsPrice = goodsInfo.totalPrice
    let makeTime = goodsInfo.createTime
    let useTime = goodsInfo.invalidTime
    let goodsName = goodsInfo.name
    let orderId = goodsInfo.orderId
    tt.navigateTo({
      url: `../refundDetail/refundDetail?orderNum=${orderNum}&orderId=${orderId}&goodsNum=${goodsNum}&goodsPrice=${goodsPrice}&makeTime=${makeTime}&useTime=${useTime}&goodsName=${goodsName}` // 
    });
  },

  /** 立即使用 */
  useClick: function (e) {
    console.log("立即使用")
    console.log(e)
    console.log(e.currentTarget.dataset.item)
    var queryBean = JSON.stringify(e.currentTarget.dataset.item)
    tt.navigateTo({
      url: `../orderDetail/orderDetail?id=${e.currentTarget.dataset.item.id}` // 指定页面的url
    });
  },

  /** 我的收益 */
  myEarningsClick: function () {
    if (this.data.earningsHidden == true) {
      return
    }
    tt.navigateTo({
      url: "../myEarnings/myEarnings" // 指定页面的url
    });
  },

  /** 页面相关事件处理函数--监听用户下拉动作 */
  onPullDownRefresh: function () {
    this.refreshData()
  },

  /** 页面刷新 */
  refreshData: function () {
    let that = this
    tt.startPullDownRefresh()
    that.setData({
      pageNo: 1,
      dataArr: [],
      hidden1: "inherit",
      hidden2: "hidden",
      hidden3: "hidden",
      state: 1,
    })
    that.loadMore()
  },

  /** 页面上拉触底事件的处理函数 */
  onReachBottom: function () {
    console.log('onReachBottom')
    let pageIndex = this.data.pageNo;
    this.setData({
      pageNo: pageIndex + 1
    })
    this.loadMore();
  },

  itemOnClick: function (event) {
    var index = event.currentTarget.dataset.id;
    this.setData({
      hidden1 : index == 1 ? "inherit" : "hidden",
      hidden2 : index == 2 ? "inherit" : "hidden",
      hidden3 : index == 3 ? "inherit" : "hidden",
      pageNo : 1
    })
    if (index == 1) {               /* 我的订单 未核销订单 */
      // this.setData({ state: -1 })
      this.setData({ state: 2 })
    } else if (index == 2) {        /* 已完成订单 */
      this.setData({ state: 4 })
    } else if (index == 3) {        /* 退款订单 */
      this.setData({ state: 3 })
    }
    this.loadMore()
  },

  loadMore: function () {
    let params = {
      state: this.data.state,
      pageNo: this.data.pageNo,
    };
    console.log(params)
    getOrderList(params).then((res) => {
      console.log('getListRequest请求成功')

      let list = (res.result && res.result.data) || [];
      console.log("list")
      console.log(list)
      this.setData({
        isLoadingText: res.result.pageNum > this.data.pageNo ? '加载中...' : '没有数据啦~'
      })
      if (this.data.pageNo > 1) {
        //追加数据
        this.setData({
          dataArr: this.data.dataArr.concat(list)
        })
      } else {
        this.setData({ dataArr: list });
        //停止下拉刷新
        tt.stopPullDownRefresh();
        console.log('dataArr')
        console.log(this.data.dataArr)
      }

      this.bottomIsLoadingHidden = this.data.dataArr.length == this.data.pageSize || this.data.dataArr.length == 0;

      this.setData({
        isEmptyData: this.data.dataArr.length > 0
      })

    }).catch((res) => {
      console.log('getListRequest请求失败')
      console.log(res)
    })
  }


})
